package com.xqls.controller;

import com.alibaba.fastjson.JSONObject;
import com.xqls.bean.CompanyBean;
import com.xqls.biz.CompanyBiz;
import com.xqls.biz.DictionaryBiz;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;
import com.xqls.util.DictionaryEnums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RequestMapping("/company")
@Controller
public class CompanyController {

	@Autowired private CompanyBiz companyBiz;
	@Autowired private DictionaryBiz dictionaryBiz;
	
	@RequestMapping(value = "/edit/{id}")
	public ModelAndView edit(@PathVariable(value="id") Integer companyId){
		ModelAndView view = new ModelAndView("company/companyEdit");
		Map<Integer, String> companyTypeMap = dictionaryBiz.queryDictionaryByParent(
				  DictionaryEnums.COMPANY_TYPE.getCode());
		view.addObject("companyTypeMap", companyTypeMap);
		if(companyId != null && !"".equals(companyId)){
			CompanyBean bean = companyBiz.queryByKey(companyId);
			view.addObject("comp", true);
			if(bean != null){
				view.addObject("bean", bean);
			}
		}else{
			view.addObject("comp", false);
		}
		return view;
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public String save(CompanyBean bean){
		return JSONObject.toJSONString(companyBiz.edit(bean));
	}
	
	@RequestMapping(value = "/getCompany",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getCompany(String blockUrl,Integer deviceType){
		return JSONObject.toJSONString(companyBiz.queryByBlock(blockUrl,deviceType));
	}
	
	@RequestMapping(value = "/companyListData")
	@ResponseBody
	public CommonResult companyListData(@RequestBody CommonPage commonPage, HttpServletRequest request, HttpServletResponse response){
		CommonResult result = companyBiz.queryByCommpage(commonPage,request,response);
		if(result.getIsSuccess()){
			result.setData(commonPage);
		}
		return result;
	}
	
	@RequestMapping(value="/remove")
	@ResponseBody
	public String remove(@RequestBody List<Integer> ids){
		return JSONObject.toJSONString(companyBiz.removeList(ids));
	}
	
	@RequestMapping("/checkIsHave")
	@ResponseBody
	public String checkIsHave(@RequestParam(value = "deviceNo") String deviceNo){
		return JSONObject.toJSONString(companyBiz.queryByDevice(deviceNo));
	}
}
